package se.quiz.cliente;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import se.quiz.bean.Usuario;
import se.quiz.cliente.conexao.HttpManager;
import se.quiz.cliente.conexao.PacoteRequisicao;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

public class Ranking extends Activity {
	
	private Tarefa tarefa;
	private ProgressBar pb;
	private ArrayList<Usuario> usuarios = new  ArrayList<Usuario>();
	private ListView lv;
	private ArrayAdapter<String> adapter;
	private JSONArray jsonArray;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ranking);
		
		lv = (ListView) findViewById(R.id.lvRanking);		
		pb = (ProgressBar) findViewById(R.id.pbRanking);	
		pb.setVisibility(View.VISIBLE);						
		tarefa = new Tarefa();
		PacoteRequisicao pacote = new PacoteRequisicao();
		pacote.setMethod("POST");
		pacote.setUri("http://"+Inicial.ipServidor+":"+Inicial.portaServidor+"/QuizServer/ServletRanking");
		pacote.setParametro("usuario", Inicial.USUARIO);		
		tarefa.execute(pacote);		
		
		Button btnVoltar = (Button) findViewById(R.id.btnVoltarRanking);
		btnVoltar.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(Ranking.this, MenuPrincipal.class));
			}
		});
	}
	
	private class Tarefa extends AsyncTask<PacoteRequisicao, String, String>{

		@Override
		protected void onPreExecute() {
		}
		
		@Override
		protected String doInBackground(PacoteRequisicao... params) {
			
			try {
				jsonArray = new JSONArray(HttpManager.getData(params[0]));				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {			
			pb.setVisibility(View.INVISIBLE);				
			
			try {
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject obj = jsonArray.getJSONObject(i);
					Usuario usuario = new Usuario();				
					usuario.setPontos(obj.getInt("pontos"));
					usuario.setUsuario(obj.getString("usuario"));				
					usuarios.add(usuario);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
			
			ArrayList<String> dados = new ArrayList<String>();
			
			for (Usuario us : usuarios) {
				dados.add(us.getUsuario()+" ->       "+us.getPontos()+" pontos");
			}
			
			adapter = new ArrayAdapter<String>(Ranking.this, R.layout.itens_ranking, dados);
			lv.setAdapter(adapter);			
		}
		
		@Override
		protected void onProgressUpdate(String... values) {
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ranking, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
